package com.skit.common.base

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.skit.common.event.EventBusLifecycleObserver
import com.skit.common.event.EventMessage
import com.skit.common.ktx.dp
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @author shuike <a href="mailto:shuike007@gmail.com">Contact me.</a>
 * @version 1.0
 * @since  2020/4/27
 */
open class BaseFragment : Fragment() {
    open val statusPadding: Boolean = false
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        EventBusLifecycleObserver(this)

        if (statusPadding && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//如果大于API21就给ContentView设置顶部Padding为StatusBar的高度
            //获取status_bar_height资源的ID
            val resourceId: Int = resources.getIdentifier("status_bar_height", "dimen", "android")
            val statusBarHeight = if (resourceId > 0) {
                //根据资源ID获取响应的尺寸值
                resources.getDimensionPixelSize(resourceId)
            } else {
                24.dp
            }
            view.setPadding(
                0, statusBarHeight,
                0,
                0
            )
        }

        initView(view)
        initConfig()
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initData()
    }

    open fun initData() {

    }

    open fun initConfig() {

    }

    open fun initView(view: View) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun event(eventMessage: EventMessage) {
    }
}